package com.ruoyi.group.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.group.domain.Orderdetail;
import com.ruoyi.group.domain.RpRepairOrder;
import com.ruoyi.group.domain.dto.DeptOrderDto;
import com.ruoyi.group.domain.dto.RepairListDto;
import com.ruoyi.group.domain.dto.RepairOrderDto;
import com.ruoyi.group.domain.dto.RepairOrderQualitiedDto;
import com.ruoyi.group.domain.dto.daomin.RepairOrderDtoMin;
import com.ruoyi.group.service.DeptOrderService;
import com.ruoyi.group.service.IRepairOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;

import java.util.List;

/**
 * 【】提交工单 ，用户查看工单，
 * 
 * @author CHL
 * @date 2022-03-07
 */


@Api(value = "/work" ,description = "用户提交工单,用户查看自己ID的工单等等")
@RestController
@RequestMapping("/work")
public class RepairOrderController extends BaseController {

    @Autowired
    private IRepairOrderService repairOrderService;

    @Autowired
    DeptOrderService deptOrderService;

    @ApiOperation(value = "/submit", httpMethod = "POST", response = AjaxResult.class)
    @PostMapping("/submit")
    public AjaxResult ajaxResult(@ApiParam(value = "提交工单", required = true)
                                 @Validated @RequestBody RpRepairOrder repairOrder) {
        //获取id
        repairOrder.setUserId(getUserId().intValue());
        //
        if (repairOrderService.OrderJoin(repairOrder)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //根据用户ID查询工单
    @ApiOperation(value = "/myorder", httpMethod = "POST", response = TableDataInfo.class)
    @PostMapping("/myorder")
    public TableDataInfo tableDataInfo(@ApiParam(value = "用户更据条件查询工单", required = true)
                                       RepairOrderDtoMin repairOrderDtoMin) {
        startPage();
        repairOrderDtoMin.setUserid(getUserId().intValue());
        String orderNO = ServletUtils.getParameter("order_no");
        String status = ServletUtils.getParameter("ro_status");
        String create_time = ServletUtils.getParameter("create_time");
        //为完成待完成
        repairOrderDtoMin.setOrderNo(orderNO);
        repairOrderDtoMin.setRoStatus(status);
        repairOrderDtoMin.setCreateTime(create_time);

        List<RepairOrderDto> list = repairOrderService.findOrder(repairOrderDtoMin);
        System.out.println(list.toString());

        return getDataTable(list);
    }

    //根据工单号查询对应的详情表
    @ApiOperation(value = "/find/order", httpMethod = "POST", response = TableDataInfo.class)
    @PostMapping("find/order/{serialnumber}")
    public AjaxResult ajaxResult( @ApiParam(value = "需要工单ID")@PathVariable String serialnumber){
       Orderdetail orderdetail =  repairOrderService.findSerialNumber(serialnumber);
       if (orderdetail!=null){
           return AjaxResult.success("查询成功",orderdetail);
       }
        return AjaxResult.error();
    }

    //根据工单号逻辑删除一个工单
    @GetMapping("/deleteOrder/{orderOn}")
    public AjaxResult ajaxResult1(@PathVariable String orderOn){
        System.out.println(orderOn);
        if (repairOrderService.delectOrder(orderOn)){
            return  AjaxResult.success();
        }
        return AjaxResult.error();
    }


    //查询质询页面的工单表
    @RequestMapping("/findQualitiedOrder")
    public TableDataInfo findQualitiedOrder(){
        startPage();
        String order_no = ServletUtils.getParameter("order_no");
        Integer qualitied_status = ServletUtils.getParameterToInt("qualitied_status");
        RpRepairOrder rpRepairOrder = new RpRepairOrder();
        rpRepairOrder.setRepairNo(order_no);
        rpRepairOrder.setQualitiedStatus(qualitied_status);
        List<RepairOrderQualitiedDto> list=repairOrderService.findQualitied(rpRepairOrder);
        return getDataTable(list);
    }
    //查询工单列表的工单
    @RequestMapping("/findListOrder")
    public TableDataInfo findListOrder(){
        startPage();
        String repairNo = ServletUtils.getParameter("repairNo");
        Integer repairStatus = ServletUtils.getParameterToInt("repairStatus");
        RpRepairOrder rpRepairOrder = new RpRepairOrder();
        rpRepairOrder.setRepairNo(repairNo);
        rpRepairOrder.setRepairStatus(repairStatus);
        List<RepairListDto> list=repairOrderService.findOrderList(rpRepairOrder);
        return getDataTable(list);
    }

//    用部门查询自己的所属的工单
    @PostMapping("/accept")
    public TableDataInfo findDepOrder(@RequestBody DeptOrderDto deptOrderDto){
        deptOrderDto.setUserid(getUserId().intValue());
        startPage();
        List<DeptOrderDto> list = deptOrderService.getfindOrderList(deptOrderDto);
        return  getDataTable(list);
    }
    @GetMapping("/findRepairById/{id}")
    public AjaxResult findRepairById(@PathVariable int id){
        RepairOrderDto repair = repairOrderService.findRepairById(id);
        return AjaxResult.success(repair);
    }

}

